'''
Created on 06/09/2010

@author: Marcelo
'''
import os

import pygame

from pygame.locals import *

import random

from Palabra import Palabra

width=1200

hight=900

screen_mode = (width, hight)



color_black = 0,0,0

main_Clock = 0







class Game:
# this gets called first
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(screen_mode)
        pygame.display.set_caption("Tipiado")
        pygame.font.init()
        self.ListPalabra = []
        self.vemo=""
        self.__milliSecondsPass=0
        self.quit = False
        self.last_Pass=0
        self.ListLetras=["si", "no" ,"nombre" ,"hola" ,"dinero" ,"auto" ,"dios" ,"cuanto" ,"como" ,"donde" ,"porque" ,"bien" ,"mal" ,"entender" ,"comer", "dormir", "tiempo", "tesoro", "nene" ,"nena" ,"correo", "trabajo", "poco", "onda", "preocupar", "esperar", "cantar", "correr", "bailar", "vaca", "burro", "perro", "gato", "loro", "amigo", "amiga", "jinete", "departamento", "capital", "ciudad", "deporte", "saltar", "nadar", "responsabilidad", "experto", "caballo", "gallina", "llover", "rayo", "calle", "todo", "nada", "fuerza", "alto", "bajo", "gordo", "flaco", "duro", "blando", "velocidad", "atardecer", "amanecer", "sol", "luna", "estrella", "aguja", "hilo", "bolsa", "papa", "tomate", "lechuga", "zanahoria", "alcohol", "refresco", "jugo", "especial", "lindo", "linda", "extraordinario", "ordinario", "jefe", "maestra", "director", "maestro", "directora", "libro", "lapicera", "cuaderno", "cartuchera", "mochila", "coche", "cartel", "luz", "oscuridad","zorro"]
        self.puntaje=0
        self.__velocidadPalabra=0.1
        self.__current_Time = 0
        self.__tiempoCaidaPalabras=5000
        self.__decrementadorVelocidad=500
        self.__PuntajeNivel=50
        self.__life=5
        self.__aumentoNivel=50
        pygame.mixer.init()
        #path="" #os.getcwd() 
        pygame.mixer.music.load("underwater.ogg")
        pygame.mixer.music.play(-1, 0.0)
        
        self.image = pygame.image.load("catEyesBG.png")#os.path.normpath(path+"/catEyesBG.png"))
        
        
# put game update code here
    def update(self,events):
        
        if self.__life>=0:       
            for event in events:
                if event.type == KEYDOWN:
                    if event.key == K_RETURN:
                        path=""#os.getcwd()
                        self.playsound("typewriter-line-break-1.ogg")
                        self.contarPuntos(self.vemo)
                        self.vemo=""
                        
                    elif event.key == K_ESCAPE:
                        self.quit=True
                    elif event.key == K_BACKSPACE:
                        path=""#os.getcwd() 
                        self.playsound("typewriter-key-1.ogg")
                        self.vemo=self.vemo[0:-1]
                    else:
                        try:
                            path=""#os.getcwd()
                            self.playsound("typewriter-key-1.ogg")
                            self.vemo=self.vemo + (chr(event.key))       
                        except:
                            path=""#os.getcwd() 
                            self.playsound("typewriter-key-1.ogg")
                            self.vemo=self.vemo
                               
            valor=(pygame.time.get_ticks())
            #milisegundos por update
            tiempoVuelta=(valor-self.__milliSecondsPass)
            self.__current_Time = self.__current_Time+tiempoVuelta
            self.__milliSecondsPass=self.__milliSecondsPass+tiempoVuelta
          
            if  self.__current_Time >=self.__tiempoCaidaPalabras :
                
                self.__current_Time = 0 
                self.insert_Palabra()
              
        
            self.palabra_Logic(tiempoVuelta)
            return
        else:
            self.quit=True
# put drawing code here
    def draw(self):
        self.screen.fill(color_black)
        self.screen.blit(self.image, (0,0))        
        texto = self.transform_Text(self.vemo)
        self.screen.blit(texto, (width-4*(width/7),hight-hight/10))
        texto=self.transform_Text("Vidas: "+(str(self.__life)))
        self.screen.blit(texto,(width-19*(width/20),hight-9*hight/10))
        texto = self.transform_Text("Puntaje: "+str(self.puntaje))
        self.screen.blit(texto, (width-19*(width/20),hight-19*hight/20))
        for palabra in self.ListPalabra:
            texto = self.transform_Text(palabra.getText())
            self.screen.blit(texto, (palabra.getX(), palabra.getY()))
        pygame.display.flip() 
        
           
             
       
# the main game loop
    def mainLoop(self):
        while not self.quit:
# handle events
            events=pygame.event.get()
            for event in events:
                if event.type == QUIT:
                    self.quit = True
            
            self.update(events)
            self.draw()
    
    def transform_Text(self,text):
        try:
            fuente = pygame.font.Font(None, 50)
            texto = fuente.render(text, 1, (255, 255, 255))
        except:
            text=""
            texto = fuente.render(text, 1, (255, 255, 255))
            
        return texto
    
    def insert_Palabra(self):
        number = random.randint(0, len(self.ListLetras))
        word=self.ListLetras[number]
        x = random.randint(width-9*width/10, width-width/10)
        y = 0
        palabra=Palabra(word,x,y)
        self.ListPalabra.append(palabra)
        
    
    def palabra_Logic(self,tiempoVuelta):
        for palabra in self.ListPalabra:
            number = palabra.getY()+ self.__velocidadPalabra * tiempoVuelta
            palabra.setY(number)
            if number > hight:
                self.__life-=1
                self.ListPalabra.remove(palabra)
            #print palabra.getY()
        if self.puntaje>= self.__PuntajeNivel:
            if self.__tiempoCaidaPalabras>0:
                self.__tiempoCaidaPalabras-=self.__decrementadorVelocidad
                self.__PuntajeNivel+=self.__aumentoNivel       
    
    def contar(self):
        number = 0
        for pal in self.ListPalabra:
            number = number + 1
        return number
    
    def contarPuntos(self,text=""):
        puntos=0
        for palabra in self.ListPalabra:
            if puntos < 1 :
                if palabra.getText()==text:
                    puntos=puntos + len(palabra.getText())
                    self.puntaje=self.puntaje + puntos
                    p=Palabra(text,0,0)
                    self.ListPalabra.remove(p)
                    break
    
    
    def playsound(self,soundfile):
        sound = pygame.mixer.Sound(soundfile)        
        sound.play()     
        
            
        
if __name__ == '__main__' :
       
    game = Game()
    game.mainLoop()
